﻿// JScript 文件
Ext.onReady(function(){
    //使用表单提示
    Ext.QuickTips.init();
    Ext.form.Field.prototype.msgTarget='side';
    
    var sm=new Ext.grid.CheckboxSelectionModel();
    
    var cm=new Ext.grid.ColumnModel([
        new Ext.grid.RowNumberer(),
        sm,
        {header:'订单号',width:110,align:'center',dataIndex:'orderNO'},
        {header:'客户名',width:110,align:'center',dataIndex:'cusLoginName'},
        {header:'总金额',width:80,align:'center',dataIndex:'totalMoney'},
        {header:'申请理由',width:200,align:'left',dataIndex:'refundThat'},
        {header:'申请时间',width:110,align:'center',dataIndex:'refundTime'},
        {header:'状态',width:120,align:'center',dataIndex:'refund',renderer:
          function(v){
            if(v==1){
                return "客户提交退/换货申请";
            }else if(v==2){
                return "审核通过，退/换货操作中...";
            }else if(v==3){
                return "退/换货完成";
            }
          }  
        }
    ]);
    
    cm.defaultSortable=true;
    
    var ds=new Ext.data.GroupingStore({
        proxy:new Ext.data.HttpProxy({
            url:'ashx/RefundManager.ashx'
        }),
        reader:new Ext.data.JsonReader({
            totalProperty:'totalProperty',
            root:'root'
        },
        [
            {name:'id',mapping:'orderID',type:'string'},
            {name:'orderNO',type:'string'},
            {name:'title',type:'string'},
            {name:'cusLoginName',type:'string'},
            {name:'totalMoney',type:'string'},
            {name:'refundThat',type:'string'},
            {name:'refundTime',type:'string'},
            {name:'refund',type:'string'}
        ])
    });
    var typeData=new Ext.data.SimpleStore({
        fields:["typeValue","text"],data:[["-1","所有"],["0","已完成"],["1","未完成"]]
    });
    var searchbar=new Ext.Toolbar({
        id:'searchbar',
        items:['订单号：',new Ext.form.TextField({
            id:'orderNO',
            width:110
        }),' ',
            '登录名：',
        new Ext.form.TextField({
        id:'loginname',
        width:110
        }),' ',
        '状态：',
        new Ext.form.ComboBox({
            id:'refund',
            width:80,
            store:typeData,
            mode:'local',
            editable:false,
            valueField:'typeValue',
            displayField:'text',
            hiddenName:'refundStatus',
            triggerAction:'all'
        }),' ',
        new Ext.Toolbar.Button({
            id:'searchbutton',
            iconCls:'icon_search',
            text:'查询',
            handler:doSearch
        })    
        ]
    });
    
    function doSearch(){
        ds.load({
            params:{
                start:0,
                limit:20,
                orderNO:Ext.get('orderNO').dom.value,
                loginname:Ext.get('loginname').dom.value,
                refund:Ext.get('refundStatus').getValue()
            }
        });
    }
    
    var paging=new Ext.PagingToolbar({
        pageSize:20,
        store:ds,
        displayInfo:true,
        displayMsg:'显示第{0}条到{1}条记录，一共{2}条',
        items:
        [
            "-"," ",{
                text:'审核通过',
                tooltip:'申请审核',
                iconCls:'icon-edit',
                handler:function(){
                    var _record=grid.getSelectionModel().getSelected();
                    if(sm.getCount()==1){
                        Check(_record,2);
                    }else{
                        Ext.Msg.alert('提示','请选择一条记录！');
                    }
                }
            },
            "-"," ",{
                text:'退/换货成功',
                tooltip:'申请成功',
                iconCls:'icon-edit',
                handler:function(){
                    var _record=grid.getSelectionModel().getSelected();
                    if(sm.getCount()==1){
                        CheckR(_record,3);
                    }else{
                        Ext.Msg.alert('提示','请选择一条记录！');
                    }
                }
            },
            "-"," ",{
                text:'详细信息',
                tooltip:'查看详细信息',
                iconCls:'icon-edit',
                handler:function(){
                    var _record=grid.getSelectionModel().getSelected();
                    if(sm.getCount()==1){
                        ShowDetail(_record.data.id);
                    }else{
                        Ext.Msg.alert('提示','请选择一条记录！');
                    }
                }
             }
         ],
         emptyMsg:"查询记录为空！"
    });
    
    var grid=new Ext.grid.EditorGridPanel({
        renderTo:'grid-div',
        cm:cm,
        ds:ds,
        sm:sm,
        tbar:searchbar,
        bbar:paging,
        height:500,
        border:true,
        bodyStyle:'width:1020px',
        stripeRows:true,
        title:'申请单信息'
    });
    
    ds.load({
        params:{start:0,limit:20}
    });
    function ShowDetail(id){
        var window_detail=new Ext.Window({
            title:'申请单详细信息',
            layout:'fit',
            width:680,
            height:480,
            resizable:false,
            autoHeight:true,
            modal:true,
            closeAction:'hide',
            plain:true,
            items:[{
                title:'弹出的窗口',
                header:false,
                html:'<iframe style="border-top-width:0px;border-left-width:0px;border-bottom-width:0px;width:728px;height:455px;border-right-width:0px;" src="RefundInfo.aspx?orderID='+ id +'" frameborder="0" width="100%" scrolling="auto" height="100%"></iframe>',
                border:false
            }]
        });
        window_detail.show();
    }
    
    function Check(record,forb){
        if(record.data.refund=="2" && record.data.refund=="3"){
            alert("此申请单已申请通过或正在申请中！");
            return;
        }
        var myMask=new Ext.LoadMask(
            Ext.getBody(),{
                msg:"Please wait..."
        });
        Ext.Ajax.request({
            url:'ashx/Refund_Del.ashx',
            params:{
                orderId:record.data.id,
                forbid:forb
            },
            success:function(response){
                ds.load();
                myMask.hide();
            },
            failure:function(){
                Ext.Msg.alert('提示','操作失败！');
                myMask.hide();
            }
        });
    }
    
    function CheckR(record,forb){
        if(record.data.refund=="2" && record.data.refund=="3"){
            alert("此申请单已申请通过或正在申请中！");
            return;
        }
        var myMask=new Ext.LoadMask(
            Ext.getBody(),{
                msg:"Please wait..."
        });
        Ext.Ajax.request({
            url:'ashx/Refund_DelR.ashx',
            params:{
                orderId:record.data.id,
                forbid:forb
            },
            success:function(response){
                ds.load();
                myMask.hide();
            },
            failure:function(){
                Ext.Msg.alert('提示','操作失败！');
                myMask.hide();
            }
        });
    }
});
